<?php
namespace app\controller\admin;

use app\BaseController;
use app\lib\CacheKey;
use app\lib\response\JSON;
use app\model\Api as ModelApi;
use app\validate\Api as ApiValidator;
use app\Request;
use think\exception\ValidateException;
use think\facade\Cache;

class Api extends BaseController
{

    public function list()
    {
       $list = ModelApi::order('group')->paginate();
        return JSON::page($list->total(),$list->items());
    }

    public function all(){
        $list = ModelApi::select();
        $data = [];
        foreach ($list as $item){
            if(!isset($data[$item->group])){
                $data[$item->group]['group'] = $item->group;
            }
            $data[$item->group]['api'][] = [
                'id'=>$item->id,
                'name'=>$item->name,
            ];
        }
        return JSON::successData(array_values($data));
    }


    public function add(Request $r){
        $params = $r->post();
        try {
            validate(ApiValidator::class)
                ->scene('add')
                ->check($params);
        } catch (ValidateException $e) {
            return JSON::error($e->getMessage());
        }
        $params['path'] = trim($params['path'],'/');
        $exists = ModelApi::where('path',$params['path'])->value('id');
        if($exists){
            return JSON::error("接口已存在");
        }
        $api = new ModelApi();
        $api->name = $params['name'];
        $api->path = $params['path'];
        $api->group = $params['group'];
        $api->save();
        Cache::delete(CacheKey::ALL_API_PATH);//删除接口缓存
        return JSON::success("添加成功");

    }

    public function edit(Request $r){
        $params = $r->post();
        try {
            validate(ApiValidator::class)
                ->scene('edit')
                ->check($params);
        } catch (ValidateException $e) {
            return JSON::error($e->getMessage());
        }
        $params['path'] = trim($params['path'],'/');
        $exists = ModelApi::where('path',$params['path'])->where('id','<>',$params['id'])->value('id');
        if($exists){
            return JSON::error("接口已存在");
        }

        $api = ModelApi::findOrFail($params['id']);
        $api->name = $params['name'];
        $api->path = $params['path'];
        $api->group = $params['group'];
        $api->save();
        Cache::delete(CacheKey::ALL_API_PATH);//删除接口缓存
        return JSON::success("修改成功");

    }

    public function delete(Request $r){
        $params = $r->post();
        try {
            validate(ModelApi::class)
                ->scene('delete')
                ->check($params);
        } catch (ValidateException $e) {
            return JSON::error($e->getMessage());
        }

        ModelApi::destroy($params['id']);
        Cache::delete(CacheKey::ALL_API_PATH);//删除接口缓存
        return JSON::success("已删除");
    }

    public function groups(){
        $groups = ModelApi::distinct(true)->column('group');
        return JSON::successData([
            'list' => $groups
        ]);
    }

}
